<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/public/header.jsp"%>
<%
    String callback = request.getParameter("callback");
    String id = request.getParameter("id") == null ? "" : request.getParameter("id");
    boolean isAdd = "".equals(id);
    int flag = 0;
    if(!isAdd){
    	flag = 1;
    }
%>

<html>
	<head>
		<title><%=isAdd?"新建仓库":"编辑仓库" %></title>
		<style>
			.td1{
				padding-top:10px;
				padding-bottom:5px;
				font-size: 12px;
				background-color: #c0ebd7;
				width:20%;
			}
			.td2{
				padding:5px;
			}
			.tdg{
			padding-top:10px;
			padding-bottom:5px;
			font-size: 12px;
			background-color: #c0ebd7;
			width:20%;
		}
		.tdb{
			padding:5px;
			width:30%;
		}
		</style>
	</head>
	<script type="text/javascript">	
		var flag = '<%=flag%>';
		var adminId="";
		var adminName="";
		$(function(){
			if(flag == '1'){
				$('#ff').form({onLoadSuccess:function(data){
					console.log(data);
					adminId=data.admin_id;
					adminName=data.admin;
				}});
				$('#ff').form("load",'<%=contextPath %>/springMVC/warehouse/getWareHouseById?id=<%=id%>');
			}
			$("#searchValue").keypress(function(e) { 
		    	// 回车键事件 
		     	if(e.which == 13) { 
		     		 doSearch();
		       	} 
		   	}); 
			jQuery("#p_dept_id").combotree({
				url:'<%=contextPath %>/springMVC/group/listDepartment',
				method:'POST',
				animate:true,
				lines:true,
				onChange:function(){
					doSearch();
				}
			});
			//加载部门
			$("#dept_id").combotree({
				url:'<%=contextPath %>/springMVC/group/listDepartment',
				method:'POST',
				animate:true,
				lines:true
			});
			$("#admin_id").combogrid({
				title:'人员选择',
				mode:"remote",
				loadMsg:'正在加载数据，请稍后......',
				panelWidth:550,
				panelHeight:400,
				url:"<%=contextPath %>/springMVC/group/listPageUser",
				idField:"id",
				textField:"truename",
				rownumbers:true,
				fitColumns:true,
				columns: [[
					{field:'id',hidden:true},
					{field:'truename',title:'姓名',width:100},
					{field:'deptName',title:'所属部门',width:100},
					{field:'privName',title:'角色',width:100}
				]],
				toolbar:"#tb",
				pageSize:10,
				pageList:[10,50,100],  
				pagination:true,
				onLoadSuccess:function(rtJson){
					if(rtJson.rtState== '1')
					{
						$.messager.alert('提示',rtJson.rtMsrg);
					
					}
				},
				onLoadSuccess:function(rtJson){
					jQuery('#admin_id').combogrid('setValue',adminId);
					jQuery('#admin_id').combogrid('setText',adminName);
				}
			});
		});
		
		function doSubmit(obj){
			var code = $('#code').val();
			var name = $('#name').val();
			if(!checkNullOREmpty(name,"仓库名称")){return;}
			var type = $('#type').combobox('getValue');
			var dept_id = $('#dept_id').combotree('getValue');
			if(!checkNullOREmpty(dept_id,"所属部门")){return;}
			/**
			var node = $('#dept_id').combotree('tree').tree('getSelected');
			if(!$('#dept_id').combotree('tree').tree('isLeaf',node.target)){
				if(type != '4'){
					$.messager.alert('提示','只能选择叶子部门！');
					return;
				}
			}
			**/
			var dept_name = $('#dept_id').combotree('getText');
			var admin_id = $('#admin_id').combogrid('getValue');
			if(!checkNullOREmpty(admin_id,"负责人")){return;}
			var admin = $('#admin_id').combogrid('getText');
			var contact = $('#contact').val();
			var address = $('#address').val();
			var param = "code="+code+"&type="+type+"&name="+name+"&dept_id="+dept_id+"&dept_name="+dept_name+"&admin_id="+admin_id+"&admin="+admin+"&contact="+contact+"&address="+address;
			$(obj).hide();
			if(flag == '0'){
				$.ajax({
					type:'POST',
					url:'<%=contextPath %>/springMVC/warehouse/addWareHouse',
					data:param,
					success:function(rtJson){
						if (rtJson.rtState == '0'){
							eval('window.opener.<%=callback%>();');
							window.close();
						}else{
							$(obj).show();
							warningInfo(rtJson.rtMsrg);
						}
					}
				});
			}
			else{
				param=param+"&id=<%=id%>";
				$.ajax({
					type:'POST',
					url:'<%=contextPath %>/springMVC/warehouse/updateWareHouse',
					data:param,
					success:function(rtJson){
						if (rtJson.rtState == '0'){
							eval('window.opener.<%=callback%>();');
							window.close();
						}else{
							$(obj).show();
							warningInfo(rtJson.rtMsrg);
						}
					}
				});
			}
		}
		
		function doSearch()
		{
			var dept_id = jQuery('#p_dept_id').combotree('getValue');
			var searchValue = jQuery('#searchValue').val();
			jQuery('#admin_id').combogrid("grid").datagrid('load', {
				searchValue : searchValue,
				dept_id:dept_id
			});
		}
	</script>
	<body>
		<div id="tb" style="padding:3px;height:auto">
			<div align="right">
				所属部门：<input  id="p_dept_id" name="p_dept_id"  style="width:150px"  class="easyui-combotree">
				查询条件：<input id="searchValue" name="searchValue" style="width:150px" title="用户名称或者用户真实姓名"/>
				<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="doSearch();"></a>
			</div>
		</div>
		<form id="ff" method="POST">
		<div style="width:90%;padding-left:50px;margin-top:50px">
			<table  style="border-collapse: collapse;" width="100%" border="1px" bordercolor="#0eb83a">
				<tr>
					<td align="right"  nowrap class="td1">
						仓库编码：
					</td>
					<td class="td2" nowrap>
						<input type="text" id="code" name="code" >
					</td>
				</tr>
				<tr>
					<td align="right" nowrap class="td1">
						仓库类型：
					</td>
					<td class="td2" nowrap>
						<select id="type" name="type" class="easyui-combobox" style="width:200px;">  
    						<option value="0">客户库</option>  
    						<option value="1">甲供库</option>  
    						<option value="2">工余料库</option>  
    						<option value="3">订单料库</option>  
    						<option value="4">厂家库</option>  
						</select>
					</td>
				</tr>
				<tr>
					<td align="right" nowrap class="td1">
						仓库名称：
					</td>
					<td class="td2" nowrap>
						<input type="text" id="name" name="name" size="26" class="easyui-validatebox" data-options="required:true">
					</td>
				</tr>
				
				<tr>
					<td align="right"  nowrap class="td1">
						所属部门：
					</td>
					<td class="td2" nowrap >
						<input id="dept_id" name="dept_id" class="easyui-combotree" style="width:200px;" data-options="required:true"/>
					</td>
				</tr>
				<tr>
					<td align="right"  nowrap class="td1">
						负责人：
					</td>
					<td class="td2" nowrap >
						<input type="text" id="admin_id" name="admin_id" style="width:200px;" data-options="required:true"/>
					</td>
				</tr>
				<tr>
					<td align="right"  nowrap class="td1">
						联系方式：
					</td>
					<td class="td2" nowrap >
						<input id="contact" name="contact" />
					</td>
				</tr>
				<tr>
					<td align="right"  nowrap class="td1">
						仓库地址：
					</td>
					<td class="td2" nowrap >
						<input id="address" name="address" size="50"/>
					</td>
				</tr>
				<tr>
					<td align="right" nowrap class="td1">
					</td>
					<td  nowrap colspan="5">
						<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:false" onclick="doSubmit(this);">保存</a>
						<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:false" onclick="javascript:window.close();">关闭</a>
					</td>
				</tr>
				
 			</table>
		</div>
		</form>
	</body>
</html>
